package com.ruoyi.config.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.config.domain.ApplicationConfig;
import com.ruoyi.config.service.IApplicationConfigService;
import com.ruoyi.config.vo.UpdatePayConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 应用配置Controller
 *
 * @author ruoyi
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/applicationConfig/applicationConfig")
public class ApplicationConfigController extends BaseController {
    @Autowired
    private IApplicationConfigService applicationConfigService;

    /**
     * 查询应用配置列表
     */
    @PreAuthorize("@ss.hasPermi('applicationConfig:applicationConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApplicationConfig applicationConfig) {
        startPage();
        List<ApplicationConfig> list = applicationConfigService.selectApplicationConfigList(applicationConfig);
        return getDataTable(list);
    }

    /**
     * 导出应用配置列表
     */
    @PreAuthorize("@ss.hasPermi('applicationConfig:applicationConfig:export')")
    @Log(title = "应用配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApplicationConfig applicationConfig) {
        List<ApplicationConfig> list = applicationConfigService.selectApplicationConfigList(applicationConfig);
        ExcelUtil<ApplicationConfig> util = new ExcelUtil<ApplicationConfig>(ApplicationConfig.class);
        util.exportExcel(response, list, "应用配置数据");
    }

    /**
     * 获取应用配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('applicationConfig:applicationConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(applicationConfigService.selectApplicationConfigById(id));
    }

    /**
     * 新增应用配置
     */
    @PreAuthorize("@ss.hasPermi('applicationConfig:applicationConfig:add')")
    @Log(title = "应用配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApplicationConfig applicationConfig) {
        return toAjax(applicationConfigService.insertApplicationConfig(applicationConfig));
    }

    /**
     * 修改应用配置
     */
    @PreAuthorize("@ss.hasPermi('applicationConfig:applicationConfig:edit')")
    @Log(title = "应用配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApplicationConfig applicationConfig) {
        return toAjax(applicationConfigService.updateApplicationConfig(applicationConfig));
    }

    /**
     * 删除应用配置
     */
    @PreAuthorize("@ss.hasPermi('applicationConfig:applicationConfig:remove')")
    @Log(title = "应用配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(applicationConfigService.deleteApplicationConfigByIds(ids));
    }


    /**
     * 修改应用配置
     */
    @PreAuthorize("@ss.hasPermi('payConfig:payConfig:add')")
    @Log(title = "支付方式配置", businessType = BusinessType.INSERT)
    @PostMapping("updateAllApplicationConfig")
    public AjaxResult updateAllApplicationConfig(@RequestBody List<UpdatePayConfigVo> payConfig) {
        return toAjax(applicationConfigService.updateAllApplicationConfig(payConfig));
    }
}
